/**
 * Hanzi Writer 包装器
 * 为 uni-app 环境提供 Hanzi Writer 功能
 */

// 导入 Hanzi Writer 库
import './hanzi.js';

// 确保全局 HanziWriter 对象可用
let HanziWriterInstance = null;

// 初始化 Hanzi Writer
export function initHanziWriter(canvasId, character, options = {}) {
  try {
    // 检查全局 HanziWriter 是否可用
    if (typeof HanziWriter === 'undefined') {
      console.error('HanziWriter 未定义，请确保正确导入了 hanzi.js');
      return null;
    }

    // 默认配置
    const defaultOptions = {
      width: 200,
      height: 200,
      padding: 5,
      showOutline: true,
      strokeColor: '#333',
      radicalColor: '#168F16',
      delayBetweenStrokes: 500
    };

    // 合并配置
    const mergedOptions = { ...defaultOptions, ...options };

    // 创建 Hanzi Writer 实例
    HanziWriterInstance = HanziWriter.create(canvasId, character, mergedOptions);
    
    return HanziWriterInstance;
  } catch (error) {
    console.error('初始化 HanziWriter 失败:', error);
    return null;
  }
}

// 获取当前实例
export function getWriter() {
  return HanziWriterInstance;
}

// 播放笔顺动画
export function animateCharacter() {
  if (HanziWriterInstance) {
    HanziWriterInstance.animateCharacter();
    return true;
  }
  return false;
}

// 开始描红练习
export function startQuiz(callbacks = {}) {
  if (HanziWriterInstance && typeof HanziWriterInstance.quiz === 'function') {
    HanziWriterInstance.quiz(callbacks);
    return true;
  }
  return false;
}

// 取消描红练习
export function cancelQuiz() {
  if (HanziWriterInstance && typeof HanziWriterInstance.cancelQuiz === 'function') {
    HanziWriterInstance.cancelQuiz();
    return true;
  }
  return false;
}

// 销毁实例
export function destroyWriter() {
  if (HanziWriterInstance && typeof HanziWriterInstance.destroy === 'function') {
    HanziWriterInstance.destroy();
    HanziWriterInstance = null;
    return true;
  }
  return false;
} 